package com.kitten.trigger.http;

import com.alibaba.fastjson.JSON;
import com.kitten.dataTypes.common.Constants;
import com.kitten.domain.auth.service.IAuthService;
import com.kitten.domain.openai.model.aggregates.ChatProcessAggregate;
import com.kitten.domain.openai.model.entity.MessageEntity;
import com.kitten.domain.openai.service.IChatService;
import com.kitten.trigger.http.dto.ChatGLMRequestDTO;
import com.kitten.trigger.http.dto.loginRspVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.stream.Collectors;

@Slf4j
@RestController
@CrossOrigin(origins = "${app.config.cross-origin}")
@RequestMapping("/api/${app.config.api-version}/chatgpt/")
public class ChatGLMAIServiceController {
    @Resource
    private IChatService chatService;

    @Resource
    private IAuthService authService;

    @PostMapping("completions")
    public ResponseBodyEmitter completionsStream(
            @RequestBody ChatGLMRequestDTO request,
            @RequestHeader("Authorization") String token,
            HttpServletResponse response) throws Exception {
        log.info("流式问答请求开始, 使用模型: {} | 提示符: {}", request.getModel(), JSON.toJSONString(request.getMessages()) );
        log.info("用户token: {}", token);
        //1. 基础配置
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        //2. 构建异步响应对象
        ResponseBodyEmitter emitter = new ResponseBodyEmitter(3 * 60 * 1000L);
        //3. 鉴权
        boolean success = authService.checkToken(token);

//        if (!token.equals("kitten")) { // !success
        if (!success){
            try {
                emitter.send(Constants.ResponseCode.TOKEN_ERROR.getCode());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            emitter.complete();
            return emitter;
        }
        //2. 构建参数
        String openId = authService.openid(token);
        ChatProcessAggregate chatProcessAggregate = ChatProcessAggregate.builder()
                .openid(openId)
                .model(request.getModel())
                .messages(request.getMessages().stream().map(entity -> MessageEntity.builder()
                        .role(entity.getRole())
                        .content(entity.getContent())
                        .build())
                        .collect(Collectors.toList()))
                .build();
        //3. 返回
        return chatService.completions(emitter, chatProcessAggregate);
    }

    @PostMapping("test")
    public String test(@RequestParam String params) {
        return "success test" + params;
    }


    @PostMapping("/api/v1/auth/login")
    public loginRspVO login(@RequestParam(value = "code") String code){
        System.out.println(code);
        return new loginRspVO("0000", "kitten");
    }

}
